﻿using System.Data.Entity;
using System.Web.Mvc;
using ECatalog.Models.Organizations;
using ECatalog.Models.UserModel;

namespace ECatalog.Controllers
{
    public class OrganizationController : Controller
    {
        private readonly IOrganizationRepository organizationRepository;
        private readonly IUserRepository userRepository;
        private readonly IOrganizationBuilder organizationBuilder;

        public OrganizationController(IOrganizationRepository organizationRepository, IUserRepository userRepository, IOrganizationBuilder organizationBuilder)
        {
            this.organizationRepository = organizationRepository;
            this.userRepository = userRepository;
            this.organizationBuilder = organizationBuilder;
        }

        [Authorize]
        public ActionResult Index()
        {
            @ViewBag.IsAdmin = userRepository.IsAdmin(User.Identity.Name);
            var organizations = organizationRepository.All();
            return View(organizations);
        }

        [Authorize]
        public ActionResult Create()
        {
            return View();
        }

        [Authorize]
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(OrganizationViewModel organizationViewModel)
        {
            @ViewBag.IsAdmin = userRepository.IsAdmin(User.Identity.Name);
            if (ModelState.IsValid)
            {
                var organization = organizationRepository.Add(organizationViewModel.Organization);
                if (organizationViewModel.UploadedFile != null)
                {
                    organizationBuilder.SaveFile(organizationViewModel.UploadedFile, organization);
                }
                return RedirectToAction("Index");
            }

            return View(organizationViewModel);
        }

        [Authorize]
        public ActionResult Edit(int id = 0)
        {
            @ViewBag.IsAdmin = userRepository.IsAdmin(User.Identity.Name);
            var organization = organizationRepository.GetOrganizationById(id);
            if (organization == null)
            {
                return HttpNotFound();
            }
            var organizationViewModel = new OrganizationViewModel
                {
                    Organization = organization
                };
            return View(organizationViewModel);
        }

        [Authorize]
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(OrganizationViewModel organizationViewModel)
        {
            @ViewBag.IsAdmin = userRepository.IsAdmin(User.Identity.Name);
            if (ModelState.IsValid)
            {
                organizationRepository.Edit(organizationViewModel.Organization);
                if (organizationViewModel.UploadedFile != null)
                {
                    organizationBuilder.SaveFile(organizationViewModel.UploadedFile, organizationViewModel.Organization);
                }
                return RedirectToAction("Index");
            }
            return View(organizationViewModel);
        }

        [Authorize]
        [HttpPost]
        public ActionResult Delete(int id)
        {
            @ViewBag.IsAdmin = userRepository.IsAdmin(User.Identity.Name);
            organizationRepository.Delete(id);
            return RedirectToAction("Index");
        }
    }
}